<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>仿栈与队列的操作</title>
    <script src="../assets/libs/jquery-2.0.3.js"></script>
</head>
<body>
    <!-- dom -->
    <ul>
        <li id="a">aoo</li>
        <li id="b">bar</li>
        <li id="c">car</li>
    </ul>
    <div id="aaron"></div>

    <!-- js -->
    <script>
        /**源码部分
         * 获取匹配元素集中的第n个元素，或将整个匹配的元素集作为一个干净的数组获取
         * 返回的是一个真实dom元素
         */ 
        // get: function( num ) {
        //     return num == null ?

        //         // Return a 'clean' array
        //         this.toArray() :

        //         // Return just the object
        //         ( num < 0 ? this[ this.length + num ] : this[ num ] );
        // },
    </script>

    <script>
        var $$ = aJquery = function(selector) {
            return new aJquery.fn.init(selector)
        }

        aJquery.fn = aJquery.prototype = {
            constructor: aJquery,
            init: function(selector) {
                this.selector = selector
                var result = document.querySelectorAll(selector)
                console.log('this :>> ', this);
                // 循环获取到的dom元素，挂载到jQuery对象上，类数组
                for (var i = 0; i < result.length; i++) {
                    this[i] = result[i];
                }
                // 定义一个length，用来访问个数
                this.length = result.length
                return this
            }
        }

        aJquery.fn.init.prototype = aJquery.fn

        // 拓展接口
        aJquery.extend = aJquery.fn.extend = function() {
            var options,
                copy,
                i = 1,
                target,
                length = arguments.length;
            
            if(length === i) {
                // 传递了一个参数，合并到jQuery对象上
                target = this
                i--
            }

            for (var i = 0; i < length; i++) {
                options = arguments[i];
                if(options != null) {
                    for (var key in options) {
                        if (Object.hasOwnProperty.call(options, key)) {
                            copy = options[key]
                            target[key] = copy
                        }
                    }
                }
            }

            return target
        }

        aJquery.fn.extend({
            get: function(num) {
                if(num == null) {
                    return [].slice.call(this)
                }
                var index = num < 0 ? (this.length + num) : num
                return this[index]
            },
            getName: function() {
                console.log(this.myName)
                return this
            },
            setName: function(name) {
                this.myName = name
                return this
            }
        })

        console.log($$("li").get(0))  // li#a
        console.log($$("li").get(1))  // li#b
        console.log($$("li").get(2))  // li#c
        console.log($$("li").get(-1))  // li#c

        console.log($$().setName('ajquery test').getName())  // 'ajquery test'
    </script>
</body>
</html>